Imports System.Data.SqlClient

Public Class frmUtilityVersionControl
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmUtilityVersionControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetImageControl(Me)

        Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgMDIForm.Images(75), Bitmap).GetHicon())
        Me.Text = "Version Control [ Current version " & gamsWebPlusMed.Version.Major & "." & gamsWebPlusMed.Version.Minor & "." & gamsWebPlusMed.Version.Build & "." & gamsWebPlusMed.Version.Revision & " ]"

        txtMajor.Text = gamsWebPlusMed.Version.Major
        txtMinor.Text = gamsWebPlusMed.Version.Minor
        txtBuild.Text = gamsWebPlusMed.Version.Build
        txtRevision.Text = gamsWebPlusMed.Version.Revision
    End Sub

    Private Sub txtMajor_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMajor.GotFocus
        txtMajor.SelectAll()
    End Sub

    Private Sub txtMinor_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMinor.GotFocus
        txtMinor.SelectAll()
    End Sub

    Private Sub txtBuild_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBuild.GotFocus
        txtBuild.SelectAll()
    End Sub

    Private Sub txtRevision_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRevision.GotFocus
        txtRevision.SelectAll()
    End Sub

    Private Sub txtFileName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFileName.GotFocus
        txtFileName.SelectAll()
    End Sub

    Private Sub txtNote_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNote.GotFocus
        txtNote.SelectAll()
    End Sub

    Private Sub txtNote_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNote.Validated
        txtNote.Text = TrimX(txtNote.Text)
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim strQuery As String
        Dim blnIsTransactionOpen As Boolean
        Dim cmdVersion As SqlCommand
        Dim rdVersion As SqlDataReader

        Try
            If Trim(txtFileName.Text) = "" Then
                MessageBox.Show("Specify file name!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtFileName.Focus()
                Exit Sub
            End If

            strQuery = "select top 1 * from utility_version_list order by record_id desc"

            cmdVersion = New SqlCommand(strQuery, gconWebPlusMed)
            rdVersion = cmdVersion.ExecuteReader
            rdVersion.Read()

            If Val(rdVersion.Item("major") & rdVersion.Item("minor") & rdVersion.Item("build") & rdVersion.Item("revision")) >= _
                Val(txtMajor.EditValue & txtMinor.EditValue & txtBuild.EditValue & txtRevision.EditValue) Then
                MessageBox.Show("Can't add a lower or equal version!" & vbCr & vbCr & "Current version is V " & _
                    gamsWebPlusMed.Version.Major & "." & gamsWebPlusMed.Version.Minor & "." & gamsWebPlusMed.Version.Build & "." & gamsWebPlusMed.Version.Revision, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
                rdVersion.Close()
                txtMajor.Focus()
                Exit Sub
            End If
            rdVersion.Close()

            If MessageBox.Show("Are you sure to update the version", gstrMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                txtMajor.Focus()
                Exit Sub
            End If

            gtrnWebPlusMed = gconWebPlusMed.BeginTransaction()
            blnIsTransactionOpen = True

            strQuery = lngAutogeneratePrimaryKey("utility_version_list", "record_id")
            strQuery = strQuery & "," & txtMajor.EditValue & "," & txtMinor.EditValue & "," & txtBuild.EditValue & "," & txtRevision.EditValue
            strQuery = strQuery & ",'" & Format(gdtCurrentDate, "dd-MMM-yyyy") & "'"
            strQuery = strQuery & ",'" & Replace(Trim(txtFileName.Text), "'", "''") & "'"
            strQuery = strQuery & "," & IIf(Trim(txtNote.Text) = "", "Null", "'" & Replace(txtNote.Text, "'", "''") & "'")
            Call InsertintoTable("utility_version_list", strQuery)

            gtrnWebPlusMed.Commit()

            blnIsTransactionOpen = False
            MessageBox.Show("Updated successfully", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Application.Restart()

            Exit Sub
        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
            End If
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub
End Class